class Solution {
public:
    int thirdMax(vector<int>& nums) {
        long long INF = 1e10, a = -INF, b = -INF, c = -INF;
        int cnt = 0;
        for (auto x : nums) {
            if (x > a)cnt++, c = b, b = a, a = x;
            else if (x<a && x>b)cnt++, c = b, b = x;
            else if (x<b && x>c)cnt++, c = x;
        }
        if (cnt < 3)return a;
        return c;
    }
};